from django.shortcuts import render, render_to_response
from django.template import RequestContext

from Common import sql_content
from PolicyData.PolicyControl import policy as policy_class

# Create your views here.

#政策
def policy(request):
    if request.method == "GET":
        data_count = sql_content.sql_content().data_count("select count(*) from data_collection.policy;")
        page_size = 10
        now_page = 1
        page_begin = 1
        page_end = 10
        end_page_begin = 0
        if (data_count % page_size == 0):
            page_count = data_count / page_size
        else:
            page_count = data_count // page_size + 1
            end_page_begin = data_count // page_size * page_size + 1
            # print(page_count,end_page_begin)
        if(request.GET.get('hidPageIndex') is not None):
            now_page = int(request.GET.get('hidPageIndex'))
            page_end = now_page * page_size
            page_begin = page_end - page_size + 1
            if(page_end > data_count):
                page_end = data_count
        page_count_list = []
        for num in range(1,page_count+1):
            page_count_list.append(num)
        print(page_begin,page_end)
        policy_fill = policy_class._Policy(page_begin, page_end).name_main()
        page_meg = {'page_count':page_count , 'page_count_list':page_count_list,'now_page':now_page}
        print('views_policy_modth', policy_fill)
        return render_to_response('policy.html', {'Policy': policy_fill,'page_meg':page_meg})
    elif request.method == "POST":
        print('this is Post!')
        now_id = int(request.POST.get('hidDetail'))
        if (now_id is not None):
            detail_fill = policy_class._Policy(0, 0).policy_sql_id(now_id)
            return render_to_response('policy_demo.html',{'detail_fill':detail_fill})

    else:
        return render_to_response('policy_demo.html')